package com.module.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.module.dialog.inter.DialogOnClickListener;
import com.module.dialog.utils.ClickType;
import com.module.dialog.utils.DialogStyle;

/**
 * @Author: dengqi
 * @CreateDate: 2020/4/22 11:09
 * @Description:
 */
public abstract class BaseBuilder<T extends BaseBuilder> implements View.OnClickListener {

    protected Context mContext;
    protected Dialog dialog; //用于dismiss

    private LinearLayout topPanel, contentPanel, bottomPanel; //上中下3个布局
    private TextView tvTitle; //标题
    private TextView btn1, btn2; //2个按钮
    private DialogParam mDialogParam; //基础参数
    private T builder;
    private View contentView;
    private DialogStyle mDialogStyle = DialogStyle.Normal;

    public BaseBuilder(Context context) {
        initView(context);
        initContentView();
    }

    /**
     * 初始化控件并setView给AlertDialog.Builder
     */
    private void initView(Context context) {
        mContext = context;
        builder = getBuilder();
        mDialogParam = new DialogParam();
        contentView = LayoutInflater.from(mContext).inflate(R.layout.dialog_base, null);
        topPanel = contentView.findViewById(R.id.topPanel);
        contentPanel = contentView.findViewById(R.id.contentPanel);
        bottomPanel = contentView.findViewById(R.id.bottomPanel);
        tvTitle = contentView.findViewById(R.id.alertTitle);
        btn1 = contentView.findViewById(R.id.button1);
        btn2 = contentView.findViewById(R.id.button2);

    }

    public abstract void initContentView();

    public abstract T getBuilder();

    protected abstract void connectView();

    public T setDialogStyle(DialogStyle dialogStyle) {
        mDialogStyle = dialogStyle;
        return builder;
    }

    public T setTitle(String title) {
        mDialogParam.title = title;
        return builder;
    }

    public T setButton1(String button1) {
        mDialogParam.button1 = button1;
        return builder;
    }

    public T setButton2(String button2) {
        mDialogParam.button2 = button2;
        return builder;
    }

    public T setContentView(View contentView) {
        mDialogParam.contentView = contentView;
        return builder;
    }

    public T setDialogOnClickListener(DialogOnClickListener listener) {
        mDialogParam.dialogOnClickListener = listener;
        return builder;
    }


    /**
     * 构建dialog
     *
     * @return
     */
    public Dialog build() {
        //上段布局-标题
        if (TextUtils.isEmpty(mDialogParam.title)) {
            topPanel.setVisibility(View.GONE);
        } else {
            topPanel.setVisibility(View.VISIBLE);
            tvTitle.setText(mDialogParam.title);
        }
        //中段布局-消息对话框/列表对话框/单选对话框/多选对话框
        if (mDialogParam.contentView == null) {
            contentPanel.setVisibility(View.GONE);
        } else {
            contentPanel.setVisibility(View.VISIBLE);
            contentPanel.addView(mDialogParam.contentView);
        }
        //下段布局-2个按钮
        if (TextUtils.isEmpty(mDialogParam.button1) && TextUtils.isEmpty(mDialogParam.button2)) {
            bottomPanel.setVisibility(View.GONE);
        }
        //-按钮1
        if (TextUtils.isEmpty(mDialogParam.button1)) {
            btn1.setVisibility(View.GONE);
        } else {
            btn1.setVisibility(View.VISIBLE);
            btn1.setText(mDialogParam.button1);
            btn1.setBackgroundResource(R.drawable.dialog_btn_one_bg);
            btn1.setOnClickListener(this);
        }
        //-按钮2
        if (TextUtils.isEmpty(mDialogParam.button2)) {
            btn2.setVisibility(View.GONE);
        } else {
            btn2.setVisibility(View.VISIBLE);
            btn2.setText(mDialogParam.button2);
            btn2.setBackgroundResource(R.drawable.dialog_btn_one_bg);
            btn2.setOnClickListener(this);
        }
        //设置一个按钮与两个按钮不同点击效果
        if(btn1.getVisibility() ==View.VISIBLE && btn2.getVisibility() ==View.VISIBLE){
            btn1.setBackgroundResource(R.drawable.dialog_btn_left_bg);
            btn2.setBackgroundResource(R.drawable.dialog_btn_right_bg);
        }
        //top bottom隐藏时，添加20dp高度
        int space = (int) mContext.getResources().getDimension(R.dimen.dialog_padding);
        int paddingTop = 0;
        int paddingBottom = 0;
        if(topPanel.getVisibility() == View.GONE){
            paddingTop = space;
        }
        if(bottomPanel.getVisibility() == View.GONE){
            paddingBottom = space;
        }
        contentPanel.setPadding(space, paddingTop,space, paddingBottom);

        //去掉圆角,去掉20dp高度
        if (mDialogStyle == DialogStyle.Bottom) {
            contentView.setBackgroundColor(Color.WHITE);
            contentPanel.setPadding(0, 0,0, 0);
            btn1.setBackgroundResource(R.drawable.dialog_btn_bg);
            btn2.setBackgroundResource(R.drawable.dialog_btn_bg);
        }

        //内容布局逻辑
        connectView();

        //创建dialog
        dialog = new Dialog(mContext, R.style.dialogStyle);
        dialog.setContentView(contentView);
        initWindow();
        return dialog;
    }

    /**
     * 设置对话框大小及位置
     * 写在contentview后面
     */
    private void initWindow() {
        Window window = dialog.getWindow();
        if (window != null) {
//            window.getDecorView().setPadding(0, 0, 0, 0);
            WindowManager.LayoutParams lp = window.getAttributes();
            if (mDialogStyle == DialogStyle.Normal) {
                normalDialogStyle(lp);
            } else {
                bottomDialogStyle(lp);
            }
//            window.setAttributes(lp);
        }

    }

    private void normalDialogStyle(WindowManager.LayoutParams lp) {
        DisplayMetrics d = Resources.getSystem().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.86); // 宽度设置为屏幕的0.8
    }

    private void bottomDialogStyle(WindowManager.LayoutParams lp) {
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.gravity = Gravity.BOTTOM;
        lp.windowAnimations = R.style.dialogAnimation;
    }


    /**
     * 按钮点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.button1) {
            if (mDialogParam.dialogOnClickListener != null) {
                mDialogParam.dialogOnClickListener.onClick(ClickType.Sure);
            }
            dialog.dismiss();
        } else if (v.getId() == R.id.button2) {
            if (mDialogParam.dialogOnClickListener != null) {
                mDialogParam.dialogOnClickListener.onClick(ClickType.Cancel);
            }
            dialog.dismiss();
        }
    }

    /**
     * 基本参数
     */
    public class DialogParam {
        public String title; //标题
        public View contentView; //自定义布局
        public String button1; //按钮1文字
        public DialogOnClickListener dialogOnClickListener; //点击事件
        public String button2; //按钮2文字

    }

}